package com.tz.online.filter;

import java.io.IOException;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import com.tz.online.book.service.IBookService;
import com.tz.online.common.PageBean;
import com.tz.online.entity.Book;
import com.tz.online.util.BeanFactory;

/**
 * Servlet Filter implementation class TuiJianBookFilter
 */
@WebFilter(dispatcherTypes = {DispatcherType.INCLUDE }
					, urlPatterns = { "*.jsp" })
public class TuiJianBookFilter implements Filter {

	private IBookService bookService;
	
	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// place your code here
		//获取推荐图书
		PageBean<Book> tuijianBook = bookService.queryByCondition(1,3, "推荐");
		System.out.println(">>>> TuiJianBookFilter,拿到推荐类图书...");
		//绑定到请求范围
		request.setAttribute("tuijianBook", tuijianBook);
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.bookService = (IBookService) BeanFactory.getBean("bookService");
	}

}
